package cn.hxzy.servlet.admin;

import cn.hxzy.entity.User;
import cn.hxzy.service.UserService;
import cn.hxzy.util.Md5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/editUser")
public class EditUserServlet extends HttpServlet {
    UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String id = req.getParameter("id");
            User user= userService.findByUserNumber(Integer.parseInt(id));
            req.setAttribute("user", user);
            req.getRequestDispatcher("/WEB-INF/jsp/admin/editUser.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String id = req.getParameter("id");
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            String s = Md5Util.MD5(password);
            System.out.println(id);
            System.out.println(name);
            System.out.println(password);
            if (id != null && id != "") {
                User user = new User(name, s,Integer.parseInt(id));

                System.out.println(user);
                userService.update(user);
            }
            resp.sendRedirect("showUser");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
